package com.smartleanx.module.hrm.sms;

import cn.hutool.json.JSONObject;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.AddShortUrlRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.AddShortUrlResponse;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.google.gson.Gson;
import darabonba.core.client.ClientOverrideConfiguration;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class SendSms {
    public static void main(String[] args) throws Exception {
        send();
//        getShortLink();
    }

    public static void getShortLink() throws ExecutionException, InterruptedException {
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                // Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
                .accessKeyId("LTAI5t9ajZe9Ju5CMsPF7nLB")
                .accessKeySecret("vxBdh7XihzwlPT1cwa7qv6bbSpd6aU")
                //.securityToken(System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")) // use STS token
                .build());

        // Configure the Client
        AsyncClient client = AsyncClient.builder()
                .region("cn-hangzhou")
                //.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient)
                .credentialsProvider(provider)
                //.serviceConfiguration(Configuration.create()) // Service-level configuration
                // Client-level configuration rewrite, can set Endpoint, Http request parameters, etc.
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                        //.setConnectTimeout(Duration.ofSeconds(30))
                )
                .build();

        AddShortUrlRequest addShortUrlRequest = AddShortUrlRequest.builder()
                .sourceUrl("https://www.laoanxin.cn/hrm/performance")
                .shortUrlName("劳安心")
                .effectiveDays("7")
                // Request-level configuration rewrite, can set Http request parameters, etc.
                // .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
                .build();

        CompletableFuture<AddShortUrlResponse> response = client.addShortUrl(addShortUrlRequest);
        AddShortUrlResponse resp = response.get();
        System.out.println(new Gson().toJson(resp));

        // Finally, close the client
        client.close();
    }

    public static void send() throws ExecutionException, InterruptedException {

        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                // Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
                .accessKeyId("LTAI5t9ajZe9Ju5CMsPF7nLB")
                .accessKeySecret("vxBdh7XihzwlPT1cwa7qv6bbSpd6aU")
                //.securityToken(System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")) // use STS token
                .build());

        // Configure the Client
        AsyncClient client = AsyncClient.builder()
                .region("cn-hangzhou")
                //.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient)
                .credentialsProvider(provider)
                //.serviceConfiguration(Configuration.create()) // Service-level configuration
                // Client-level configuration rewrite, can set Endpoint, Http request parameters, etc.
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                        //.setConnectTimeout(Duration.ofSeconds(30))
                )
                .build();

        //1. 模板code ：SMS_489835579   SMS_492390492（url）  SMS_492390492(laoanxin)
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("signlink","");
        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .phoneNumbers("18259479681")
                .signName("青岛劳安智心科技")
                .templateCode("SMS_492390492")
                .templateParam(jsonObject.toString())
                .build();

        // Asynchronously get the return value of the API request
        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
        // Synchronously get the return value of the API request
        SendSmsResponse resp = null;
        resp = response.get();
        System.out.println(new Gson().toJson(resp));
    }
}
